package com.wz.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wz.entity.Emp;
import com.wz.entity.User;
import com.wz.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserHtmlController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("addUser")
    public String toadd(){
        return "addUser";
    }

    private PageInfo<User> getPageInfo(){
        PageHelper.startPage(1, 10);
        List<User> users = userService.findUsers();
        PageInfo<User> pageInfo = new PageInfo<>(users);//使用PageInfo包装类
        return pageInfo;

    }


    @PostMapping("addUser1")
    public String save2(User user, HttpServletRequest request) {
        userService.save(user);
        request.getSession().setAttribute("pageInfo3",getPageInfo());
        return "showuser";
    }
    @RequestMapping("/delete/{id}")
    public String delte(@PathVariable("id") String id, HttpServletRequest request) {
        userService.delete(id);
        System.out.println("delete方法执行成功！！");
        request.getSession().setAttribute("pageInfo3",getPageInfo());
        return "showuser";
    }


}

